<template>
	<el-form ref="loginForm" :model="form" :rules="rules" label-width="0" size="large">
		<el-form-item prop="username">
			<el-input ref="username" v-model="form.username" prefix-icon="el-icon-user" clearable
					  :placeholder="$t('login.userPlaceholder')">
			</el-input>
		</el-form-item>
		<el-form-item prop="password">
			<el-input ref="password" v-model="form.password" prefix-icon="el-icon-lock" clearable show-password
					  :placeholder="$t('login.PWPlaceholder')"
					  @keyup.enter="login"></el-input>
		</el-form-item>
		<el-form-item style="margin-bottom: 10px;">
			<el-col :span="12">
				<el-checkbox :label="$t('login.rememberMe')" v-model="form.autologin"></el-checkbox>
			</el-col>
			<el-col :span="12" class="login-forgot">
				<router-link to="/reset_password">{{ $t('login.forgetPassword') }}？</router-link>
			</el-col>
		</el-form-item>
		<el-form-item>
			<el-button type="primary" style="width: 100%;" :loading="islogin" round @click="login">{{
					$t('login.signIn')
				}}
			</el-button>
		</el-form-item>
		<div class="login-reg">
			{{ $t('login.noAccount') }}
			<router-link to="/user_register">{{ $t('login.createAccount') }}</router-link>
		</div>
	</el-form>
</template>

<script>
export default {
	data() {
		return {
			form: {
				username: "admin",
				password: "admin",
				autologin: false
			},
			rules: {
				username: [
					{required: true, message: this.$t('login.userError'), trigger: 'blur'}
				],
				password: [
					{required: true, message: this.$t('login.PWError'), trigger: 'blur'}
				]
			},
			islogin: false,
		}
	},
	watch: {
		'form.autologin'(val) {
			console.log(val)
			if (val) {
				this.$TOOL.cookie.set("AUTO_LOGIN", val, {
					expires: 60 * 60 * 12
				})
			} else {
				this.$TOOL.cookie.remove("AUTO_LOGIN");
			}
		}
	},
	created() {

	},
	mounted() {
		if (this.form.username === '') {
			this.$refs.username.focus()
		} else {
			this.$refs.password.focus()
		}

		let autologin = Boolean(this.$TOOL.cookie.get("AUTO_LOGIN"));
		if (autologin && this.$TOOL.cookie.get("TOKEN")) {
			this.form.autologin = true;
		} else {
			this.form.autologin = false;
		}
	},
	methods: {

		/**
		 * 登陆
		 * 登陆后 需要记录信息如下：
		 * cookie:  TOKEN、TOKEN_TTL
		 * localStorage:  USER_INFO、MENU、PERMISSIONS
		 *
		 * @returns {Promise<boolean>}
		 */
		async login() {
			let validate = await this.$refs.loginForm.validate().catch(() => {
			})
			if (!validate) {
				return false
			}
			this.islogin = true
			// 登陆
			let userLoginResult = await this.$API.auth.login.post(this.form)
			if (userLoginResult.status != 200) {
				this.islogin = false
				return false
			}
			let ttl = userLoginResult.data.tokenExpirationTime * 60
			this.$TOOL.cookie.set("TOKEN", userLoginResult.data.tokenValue, {
				expires: ttl
			})
			this.$TOOL.cookie.set("TOKEN_TTL", ttl)
			let userInfo = userLoginResult.data.userInfo;
			let uInfo = {
				userId: userInfo.userId,
				userName: userInfo.username,
				nickname: userInfo.nickname,
				headPortrait: userInfo.headPortrait,
				dashboard: "0",
				role: []
			}
			for (let ur of userLoginResult.data.userInfo.roles) {
				uInfo.role.push(ur.name);
			}
			this.$TOOL.data.set("USER_INFO", uInfo)

			// 获取登陆用户可访问的菜单权限
			var menu = await this.$API.system.menu.myMenus.get()
			if (menu.status != 200) {
				this.islogin = false
				this.$message.warning(menu.message)
				return false
			}
			if (menu.data.menu.length == 0) {
				this.islogin = false
				this.$alert("当前用户无任何菜单权限，请联系系统管理员", "无权限访问", {
					type: 'error',
					center: true
				})
				return false
			}
			this.$TOOL.data.set("MENU", menu.data.menu)
			this.$TOOL.data.set("PERMISSIONS", menu.data.permissions)

			this.$router.replace({
				path: '/'
			})
			this.$message.success("Login Success 登录成功")
			this.islogin = false
		},
	}
}
</script>

<style>
</style>
